package top.tangyh.lamp.care.mapper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BCareRecord;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.query.CareRecordQueryDto;
import top.tangyh.lamp.care.vo.result.FanShenRecordOutputDTO;
import top.tangyh.lamp.care.vo.save.FanShenRecordInputDTO;
import top.tangyh.lamp.care.vo.update.UpdateCareRecordStatusVO;
import top.tangyh.lamp.common.constant.CommonConstant;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * Mapper 接口
 * 护理操作记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-18 14:10:32
 * @create [2024-07-18 14:10:32] [zqb] [代码生成器生成]
 */
@Repository
public interface BCareRecordMapper extends SuperMapper<BCareRecord> {

    /**
     * 是否存在有效记录
     *
     * @param param 查询参数
     * @return
     */
    default boolean existVaildRecord(CareRecordQueryDto param) {
        return CollectionUtil.isNotEmpty(getValidList(param));
    }

    /**
     * 获得护理操作记录
     *
     * @param param 杨明询参数
     * @return
     */
    default List<BCareRecord> getValidList(CareRecordQueryDto param) {
        LambdaQueryWrapper<BCareRecord> queryWrapper = newLambdaQueryWrapper(param);
        queryWrapper.eq(BCareRecord::getIsDeleted, CommonConstant.NO_NUMBER);
        return selectList(queryWrapper);
    }

    /**
     * 查询参数转换
     *
     * @param param 需转换查询参数
     * @return
     */
    default LambdaQueryWrapper<BCareRecord> newLambdaQueryWrapper(CareRecordQueryDto param) {
        LambdaQueryWrapper<BCareRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(param.getHospitalNumber()), BCareRecord::getHospitalNumber, param.getHospitalNumber());
        queryWrapper.eq(StrUtil.isNotBlank(param.getItemName()), BCareRecord::getItemName, param.getItemName());
        queryWrapper.eq(param.getMappedTime() != null, BCareRecord::getMappedTime, param.getMappedTime());
        return queryWrapper;
    }

    default List<BCareRecord> selectByItemIdAndMapperTime(Long itemId,Long mappedTime,String patientIdent){
        LambdaQueryWrapper<BCareRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCareRecord::getItemId,itemId)
                .eq(BCareRecord::getMappedTime,mappedTime)
                .eq(BCareRecord::getPatientIdent,patientIdent);
        return selectList(queryWrapper);
    }

    /**
     * 更新护理操作删除状态
     *
     * @param updateStatusVO
     */
    default void updateStatus(UpdateCareRecordStatusVO updateStatusVO) {
        String hospitalNumber = updateStatusVO.getHospitalNumber();
        List<Long> mappedTimeList = updateStatusVO.getMappedTimeList();
        Integer deleteStatus = updateStatusVO.getDeleteStatus();
        LambdaQueryWrapper<BCareRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCareRecord::getHospitalNumber, hospitalNumber)
                .in(BCareRecord::getMappedTime, mappedTimeList);
        BCareRecord yxCareRecord = new BCareRecord();
        yxCareRecord.setIsDeleted(deleteStatus);
        this.update(yxCareRecord, queryWrapper);
    }


    /**
     * 获取翻身记录
     *
     * @param fanShenRecordInputDTO
     * @return
     */
    List<FanShenRecordOutputDTO> queryFanShenRecord(FanShenRecordInputDTO fanShenRecordInputDTO);

    default void delCellByTimePiont(String patientIdent, String hospitalNumber, LocalDateTime timePoint){
        LambdaQueryWrapper<BCareRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCareRecord::getMappedTime, PDateUtils.getTimestampOfDateTime(timePoint) )
                .eq(BCareRecord::getHospitalNumber,hospitalNumber)
                .eq(BCareRecord::getPatientIdent,patientIdent);
        delete(queryWrapper);
    }

    default void deleteOlds(String patientIdent, Long recordTime, Set<String> hlczcodes, String operator){
        LambdaQueryWrapper<BCareRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCareRecord::getPatientIdent, patientIdent)
                .in(BCareRecord::getItemCode, hlczcodes)
                .eq(BCareRecord::getCreatedBy,operator)
                .eq(BCareRecord::getMappedTime, recordTime);
        delete(queryWrapper);
    }

    
}


